package com.vip.xchat.service.impl;

import cn.novelweb.tool.date.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vip.file.model.response.ErrorCode;
import com.vip.file.model.response.Result;
import com.vip.file.model.response.Results;
import com.vip.file.utils.UuidUtils;
import com.vip.xchat.mapper.UserMapper;
import com.vip.xchat.mapper.XChatMapper;
import com.vip.xchat.mapper.XSessionMapper;
import com.vip.xchat.model.Xchat;
import com.vip.xchat.model.Xsession;
import com.vip.xchat.model.Xuser;
import com.vip.xchat.service.IXChatService;
import com.vip.xchat.service.IXSessionService;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.IdGenerator;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class XSessionServiceImpl implements IXSessionService {

    @Autowired
    private XSessionMapper xSessionMapper;

    @Override
    public Result<Object> addSeesion(Xsession xsession) {
        String userId = xsession.getUserId();
        if (StringUtils.isEmpty(userId))
            return Results.newFailResult(ErrorCode.INVALID_PARAMETER, "参数 userId 不可为空!");
        String sessionId = UuidUtils.uuid();
        xsession.setSessionId(sessionId);
        xSessionMapper.insert(xsession);
        return Results.newSuccessResult(xsession, "会话创建成功!");
    }

    @Override
    public Result<Object> getSessionList(Xsession xsession) {
        String userId = xsession.getUserId();
        if (StringUtils.isEmpty(userId))
            return Results.newFailResult(ErrorCode.INVALID_PARAMETER, "参数 userId 不可为空!");
        List<Xsession> sessionLisst = xSessionMapper.getSessionList(userId);
        return Results.newSuccessResult(sessionLisst, "获取会话列表成功!");
    }

    @Override
    public String getUserIdsBySession(String sessionId) {
        String userIds = xSessionMapper.getUserIdsBySessionId(sessionId);
        if (!StringUtils.isEmpty(userIds))
            return userIds;
        return "";
    }

}
